﻿@model List<OSBLE.Models.HomePage.Event>
@using OSBLE.Models.HomePage
@{
    DateTime lastDate = DateTime.Parse("1/1/1990");
}
<ul id="event_list">
    @foreach (Event e in Model)
    {
        if (lastDate.Date != e.StartDate.Date)
        {
            lastDate = e.StartDate.Date;   
        <li class="event_date">
            @ShowDate(lastDate)
        </li>
        }
        <li id="event_@(e.ID)" class="event">
            <div class="event_tools">
                @if (ViewBag.ActiveCourse.AbstractCourse.AllowEventPosting)
                {
                    <a href="@Url.Action("Create", "Event", new { start = e.StartDate })" title="Add Event At This Time">@Helpers.AddButton()</a>
                }
                @if (ViewBag.ActiveCourse.AbstractRole.CanModify && !e.HideDelete)
                {
                    <a title="Modify This Event" href="@Url.Action("Edit", "Event", new { ID = e.ID })">@Helpers.EditButton()</a>
                }
                @if (ViewBag.ActiveCourse.AbstractRole.CanModify && !e.HideDelete)
                {
                    <form action="@Url.Action("Delete", "Event")"
                        style="display: inline;"
                        data-ajax="true"
                        data-ajax-success="$('#event_@(e.ID)').hide('blind',{},'slow',function(){$(this).remove();})"
                        data-ajax-confirm="Are you sure you want to delete this event?"
                        method="post">
                    <input type="hidden" name="ID" value="@e.ID" />
                    @Helpers.DeleteSubmit("Delete This Event")
                    </form>
                }
            </div>
            @if (!e.NoDateTime)
            {
                <div class="event_time">
                    <small>
                        @ShowEventTime(e.StartDate, e)
                        @if ((e.EndDate != null))
                        {
                            DateTime dt = (DateTime)e.EndDate;

                            //Event that lasts only one day
                            if (dt.Date.CompareTo(e.StartDate.Date) == 0)
                            {
                                <text>&mdash; @ShowTime(dt)</text>
                            }

                            //display day as well as time for multi-day spanning events
                            else
                            {
                                <text>&mdash; @ShowDayAndTime(dt)</text>
                            }

                        }
                    </small>
                </div>
            }
            <div>
                <small><b>@e.Title</b></small>
            </div>
            @if (e.Description != null)
            {
                <div class="event_description">
                    @{
                string description = Helpers.Linkify(e.Description).ToHtmlString();
                description = description.Replace("\n", "<br />");
                    }
                    <small>@Html.Raw(description)</small>
                </div>
            }
        </li>
    }
</ul>

@helper ShowDayAndTime(DateTime dt)
    {
    @dt.ToString("ddd @ h:mm tt");
}

@helper ShowTime(DateTime dt)
    {
        @dt.ToString("h:mm tt");
}
@helper ShowEventTime(DateTime dt, Event e)
    {
        if (!e.HideTime)
        {
            @dt.ToString("h:mm tt");
        }
}
@helper ShowDate(DateTime dt)
    {
    @dt.ToString("dddd, MMMMM d, yyyy");
}